package com.itcm.purchase.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcm.purchase.common.R;
import com.itcm.purchase.common.utils.DoubleCalcUtil;
import com.itcm.purchase.common.utils.PageUtil;
import com.itcm.purchase.entity.SaleContract;
import com.itcm.purchase.entity.dto.PageDto;
import com.itcm.purchase.service.SaleContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 销售合同 前端控制器
 * </p>
 *
 * @author cm_fighting
 * @since 2020-07-28
 */
@RestController
@RequestMapping("/purchase/sale-contract")
@CrossOrigin
@Api(tags = "销售合同")
public class SaleContractController {

    @Autowired
    private SaleContractService contractService;

    @PostMapping("save")
    @ApiOperation(value = "保存销售合同记录")
    public R save(@RequestBody SaleContract contract) {
        if (null != contract.getPrice() && null != contract.getNum()) {
//            contract.setTotalAmount(contract.getPrice() * contract.getNum());
            contract.setTotalAmount(DoubleCalcUtil.mul(contract.getPrice(), contract.getNum()));
        }
        boolean b = contractService.saveOrUpdate(contract);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @PostMapping("listPage")
    @ApiOperation(value = "分页查询销售合同信息")
    public R listPage(@RequestBody PageDto page, String orderDate) {
        Page<SaleContract> contractPage = PageUtil.getPageParam(page, SaleContract.class);
        contractService.listPage(contractPage, orderDate);
        long total = contractPage.getTotal();
        List<SaleContract> records = contractPage.getRecords();
        return R.ok().data("total", total).data("items", records);
    }

    @ApiOperation(value = "批量删除")
    @PostMapping(value = "delBatchIds")
    public R delBatchIds(@RequestParam String ids) {
        String[] idstr = ids.split(",");
        for (String s : idstr) {
            contractService.removeById(Long.valueOf(s));
        }
        return R.ok();
    }

    @ApiOperation(value = "获取已存在科目名称列表")
    @RequestMapping(value = "listName", method = {RequestMethod.GET, RequestMethod.POST})
    public R getExistsName() {
        List<String> list = contractService.listName();
        return R.ok().data("list", list);
    }


}

